package com.qeeyou.apps.android.craftsman;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qeeyou.apps.android.craftsman.activity.login.LoginActivity;
import com.qeeyou.apps.android.craftsman.event.LogoutEvent;
import com.qeeyou.apps.android.craftsman.fragment.HomeFragment;
import com.qeeyou.apps.android.craftsman.fragment.MeFragment;
import com.qeeyou.apps.android.craftsman.fragment.MessageFragment;
import com.qeeyou.apps.android.craftsman.fragment.OrderFragment;
import com.qeeyou.apps.android.craftsman.fragment.PublishFragment;
import com.qeeyou.apps.android.craftsman.service.SystemContext;
import com.qeeyou.apps.android.craftsman.view.NoScrollViewPager;
import com.qeeyou.apps.android.craftsman.view.TabbarButton;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private static final String TAG = "MainActivity";

    private NoScrollViewPager viewPager;
    private LinearLayout llayoutTabbar;
    private ImageView imgvPublish;

    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //TODO -- 初始化
        SystemContext.getInstance().init(MainActivity.this.getApplicationContext());

        EventBus.getDefault().register(this);

        setView();
        setEvent();
    }

    private void setView() {
        viewPager = (NoScrollViewPager) this.findViewById(R.id.viewpager4Main);
        llayoutTabbar = (LinearLayout) this.findViewById(R.id.llayoutTabbar4Main);
        imgvPublish = (ImageView) this.findViewById(R.id.imgvPublish4Main);
    }

    private void setEvent() {

        fragments = new ArrayList<Fragment>();
        fragments.add(new HomeFragment());
        fragments.add(new OrderFragment());
        fragments.add(new MessageFragment());
        fragments.add(new MeFragment());
        fragments.add(new PublishFragment());

        viewPager.setAdapter(new MyFragmentAdapter(this.getSupportFragmentManager(), fragments));
        viewPager.setOffscreenPageLimit(3);
        viewPager.setHorizontalScrollable(false);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //默认选中首页
        ((TabbarButton) llayoutTabbar.getChildAt(0)).setSelected(true);

        final int tabCount = llayoutTabbar.getChildCount();
        for (int i = 0; i < tabCount; i++) {
            final TabbarButton tabbarButton = (TabbarButton) llayoutTabbar.getChildAt(i);
            final int currentIndex = i;
            tabbarButton.setTag(i);
            tabbarButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (view.isSelected()) {
                        //消除重复选中
                        return;
                    }

                    if (!SystemContext.getInstance().isLogin() && currentIndex > 0) {
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        MainActivity.this.startActivity(intent);
                        return;
                    }

                    //重置选中状态
                    for (int j = 0; j < tabCount; j++) {
                        ((TabbarButton) llayoutTabbar.getChildAt(j)).setSelected(false);
                    }
                    tabbarButton.setSelected(true);
                    Integer index = (Integer) view.getTag();
                    viewPager.setCurrentItem(index, false);
                }
            });
        }

        //发布按钮点击事件
        imgvPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!SystemContext.getInstance().isLogin()) {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    MainActivity.this.startActivity(intent);
                    return;
                }
                for (int j = 0; j < tabCount; j++) {
                    ((TabbarButton) llayoutTabbar.getChildAt(j)).setSelected(false);
                }
                viewPager.setCurrentItem(4, false);
            }
        });

        ((TabbarButton) llayoutTabbar.getChildAt(0)).performClick();
    }


    //MyFragmentAdapter
    public class MyFragmentAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public MyFragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }

    //在产生事件的线程中执行
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onLoginEvent(LogoutEvent event) {
        viewPager.setCurrentItem(0);
        ((TabbarButton) llayoutTabbar.getChildAt(0)).setSelected(true);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
